/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utilities;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author omar
 */
public class MovingAverage extends TransiantRemoval
{
    private String replicasFilePath ;
    private ArrayList<Double> meanTrajectory ;
    private ArrayList<Double> movingAverage ;
    private ArrayList<ArrayList<Double>>  data;
    private int noOfReplicas;

    public MovingAverage(String replicasFilePath)
    {
        this.replicasFilePath = replicasFilePath;
        init();
    }

    private void init()
    {
        try
        {
            File file = new File(replicasFilePath);
            String [] replicaSNames = file.list();
            noOfReplicas = replicaSNames.length;
            // then start to load data from files
            data = new ArrayList<ArrayList<Double>>();
            for (int i = 0; i < replicaSNames.length; i++)
            {
                    String string = replicaSNames[i];
                    data.add(super.readFile(string));
            }
        }
        catch (FileNotFoundException ex)
        {
            Logger.getLogger(InitialDataDeletion.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IOException ex)
        {
            Logger.getLogger(InitialDataDeletion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void calculateMeanTrajectory()
    {
        meanTrajectory = new ArrayList<Double>();
        for (int i = 0; i < NO_OF_RV; i++)
        {
            meanTrajectory.add((double) (1 / noOfReplicas) * getSum(i));
        }
    }

    private double getSum(int j)
    {
        double res = 0 ;
        for (int i = 0; i < data.size(); i++)
        {
            ArrayList<Double> list = data.get(i);
            res += list.get(j);
        }
        return res;
    }

    public void calculateMovingAverage(int k)
    {
        double sum = 0;
        for (int i = k + 1; i < NO_OF_RV - k; i++)
        {
            for (int j = -k; j < k; j++)
            {
                sum += meanTrajectory.get(i + j - 1);
            }
            movingAverage.add((double) (1/(2*k + 1)) * sum );
            sum = 0;
        }
        try
        {
            super.saveData("movingAverageWithK=" + k, movingAverage, k + 1);
        } catch (IOException ex)
        {
            Logger.getLogger(MovingAverage.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}